package com.framework.modules.sys.controller;

import com.framework.common.annotation.SysLog;
import com.framework.exception.util.RespResult;
import com.framework.modules.sys.service.YhbRoleDeptService;
import com.framework.modules.sys.service.YhbSysRoleMenuService;
import com.framework.modules.sys.service.YhbSysRoleService;
import com.framework.page.PageUtils;
import com.framework.validator.ValidatorUtils;
import com.framework.yhbEntity.sys.YhbSysRole;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;


/**
 * 角色管理
 */
@RestController
@RequestMapping("/sys/role")
public class SysRoleController extends AbstractController {
	@Autowired
	private YhbSysRoleService yhbSysRoleService;
	@Autowired
	private YhbSysRoleMenuService yhbSysRoleMenuService;
	@Autowired
	private YhbRoleDeptService yhbRoleDeptService;

	/**
	 * 角色列表
	 */
	@RequestMapping("/list")
	@RequiresPermissions("sys:role:list")
	public RespResult list(@RequestParam Map<String, Object> params) {
		PageUtils page = yhbSysRoleService.queryPage(params);

		return RespResult.ok().put("page", page);
	}

	/**
	 * 角色列表
	 */
	@RequestMapping("/select")
	@RequiresPermissions("sys:role:select")
	public RespResult select() {
		List<YhbSysRole> list = yhbSysRoleService.selectList(null);

		return RespResult.ok().put("list", list);
	}

	/**
	 * 角色信息
	 */
	@RequestMapping("/info/{roleId}")
	@RequiresPermissions("sys:role:info")
	public RespResult info(@PathVariable("roleId") Long roleId) {
        YhbSysRole role = yhbSysRoleService.selectById(roleId);

		// 查询角色对应的菜单
		List<Long> menuIdList = yhbSysRoleMenuService.queryMenuIdList(roleId);
		role.setMenuIdList(menuIdList);

		// 查询角色对应的部门
		List<Long> deptIdList = yhbRoleDeptService.queryDeptIdList(new Long[] { roleId });
		role.setDeptIdList(deptIdList);

		return RespResult.ok().put("role", role);
	}

	/**
	 * 保存角色
	 */
	@SysLog("保存角色")
	@RequestMapping("/save")
	@RequiresPermissions("sys:role:save")
	public RespResult save(@RequestBody YhbSysRole role) {
		ValidatorUtils.validateEntity(role);

        yhbSysRoleService.save(role);

		return RespResult.ok();
	}

	/**
	 * 修改角色
	 */
	@SysLog("修改角色")
	@RequestMapping("/update")
	@RequiresPermissions("sys:role:update")
	public RespResult update(@RequestBody YhbSysRole role) {
		ValidatorUtils.validateEntity(role);

        yhbSysRoleService.update(role);

		return RespResult.ok();
	}

	/**
	 * 删除角色
	 */
	@SysLog("删除角色")
	@RequestMapping("/delete")
	@RequiresPermissions("sys:role:delete")
	public RespResult delete(@RequestBody Long[] roleIds) {
        yhbSysRoleService.deleteBatch(roleIds);

		return RespResult.ok();
	}
}
